`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    22:12:02 05/01/2014 
// Design Name: 
// Module Name:    status_bars 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module status_bars(	bullet,pheonix,character,
							sprite1,sprite2,sprite3,sprite4,sprite5,sprite6,sprite7,sprite8,
							enablecat1,enablecat2,enablecat3,enablecat4,enablecat5,enablecat6,enablecat7,enablecat8,
							blank,hcount,vcount,clk,rst,
							HP_Bar_pheonix,HP_Bar_inverse,HP_Bar_Border,
							boss_enable,splash_enable,reset_score,score_off,hp_off,
							score_bar,score_border);
	//////////////////////////////////////////////////////////////////////////////////
	input clk,rst,blank,bullet,pheonix, character, reset_score, score_off,hp_off;
	input sprite1,sprite2,sprite3,sprite4,sprite5,sprite6,sprite7,sprite8;
	input [10:0] hcount,vcount;
	//////////////////////////////////////////////////////////////////////////////////
	output wire	enablecat1,enablecat2,enablecat3,enablecat4,enablecat5,enablecat6,enablecat7,enablecat8;
	output 	HP_Bar_pheonix,
				HP_Bar_inverse,
				HP_Bar_Border,
				score_bar,
				score_border,
				boss_enable,
				splash_enable;
	//////////////////////////////////////////////////////////////////////////////////
	parameter Pheonix_start_HP = 150;
	parameter enemy_kill_goal = 20;
	parameter Xpheonix_HP_Bar = 160;	// initial x position of pheonix hp bar
	parameter Ypheonix_HP_Bar = 7;	// initial y position of pheonix hp bar
	parameter Xscore_bar = 540;
	parameter Yscore_bar = 440;
	
	//////////////////////////////////////////////////////////////////////////////////
	reg hit_cat1, hit_cat2, hit_cat3, hit_cat4, hit_cat5, hit_cat6, hit_cat7, hit_cat8;
	reg [4:0] enemies_killed; 	// players score is # enemies killed
	reg boss_mode;
	reg splash_mode;
	reg wombat_hurt;
	
	reg [7:0] HPreg_pheonix;
	reg same_hit;
	
	initial begin
		hit_cat1 <= 1'b0; 
		hit_cat2 <= 1'b0; 
		hit_cat3 <= 1'b0; 
		hit_cat4 <= 1'b0; 
		hit_cat5 <= 1'b0; 
		hit_cat6 <= 1'b0; 
		hit_cat7 <= 1'b0; 
		hit_cat8 <= 1'b0; 
		
		boss_mode <= 1'b0;
		splash_mode <= 1'b0;
		enemies_killed <= 1'b0;
		wombat_hurt <= 1'b0;
		
		HPreg_pheonix <= Pheonix_start_HP;
		same_hit <= 0;
	end
	
	
	always @(posedge clk) begin
		if (rst) begin
			splash_mode <= 1'b0;
		end else if (HPreg_pheonix < 10) begin
			splash_mode <= 1'b1;
		end else if (wombat_hurt) begin
			splash_mode <= 1'b1;
		end else if (character & pheonix) begin
			splash_mode <= 1'b1;
		end if (!score_off) begin
			splash_mode <= 1'b0;
		end 
	end

	// -------- HERE THE WOMBAT TAKES DAMAGE -----------
	always @(posedge clk) begin
		if (rst) begin
			wombat_hurt <= 1'b0;
		end
		else if (pheonix & character) begin	//detected collision of character and boss
			wombat_hurt <= 1'b1;
		end else if (an_enemy_sprite & character) begin	//detected collision of character and player
			wombat_hurt <= 1'b1;
		end else
			wombat_hurt <= 1'b0;
	end
	
	// -------- LETS INCREASE SCORE WHEN HIT CAT -------
	always @ (posedge clk) begin
		if (rst) hit_cat1 <= 1'b0; 
		else if (bullet & sprite1 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat1 <= 1'b1;
		end else
			hit_cat1 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat2 <= 1'b0; 
		else if (bullet & sprite2 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat2 <= 1'b1;
		end else
			hit_cat2 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat3 <= 1'b0; 
		else if (bullet & sprite3 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat3 <= 1'b1;
		end else
			hit_cat3 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat4 <= 1'b0; 
		else if (bullet & sprite4 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat4 <= 1'b1;
		end else
			hit_cat4 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat5 <= 1'b0; 
		else if (bullet & sprite5 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat5 <= 1'b1;
		end else
			hit_cat5 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat6 <= 1'b0; 
		else if (bullet & sprite6 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat6 <= 1'b1;
		end else
			hit_cat6 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat7 <= 1'b0; 
		else if (bullet & sprite7 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat7 <= 1'b1;
		end else
			hit_cat7 <= 1'b0;
	end
	
	always @ (posedge clk) begin
		if (rst) hit_cat8 <= 1'b0; 
		else if (bullet & sprite8 || wombat_hurt) begin		//detected collision of projectile and cat
			hit_cat8 <= 1'b1;
		end else
			hit_cat8 <= 1'b0;
	end
	// --- WHEN DO WE INCREASE THE SCORE?-----
	always @ (posedge clk) begin
		if (rst) enemies_killed <= 0;
		else if ((an_enemy_sprite & bullet) && enemies_killed < enemy_kill_goal )
			enemies_killed <= enemies_killed + 1'b1;
		else if (enemies_killed >= enemy_kill_goal) begin
			enemies_killed <= enemy_kill_goal; 	// make sure the score doesnt go too high
		end else if (reset_score) begin
			enemies_killed <= 0;
		end
	end
			
			
	always @ (posedge clk) begin
		if (rst) begin
			boss_mode <= 0;
		end else if (enemies_killed == enemy_kill_goal) begin
			boss_mode <= 1'b1;		// killed enough enemies, now enter boss mode!
		end else if (wombat_hurt) begin
			boss_mode <= 1'b0;		// if user got hurt, disable boss mode
		end else begin
			boss_mode <= 1'b0;
		end
	end
	
	
	// -------- TEST: DECREASE HP OF BOSS-------
	always @(posedge clk) begin
		if (rst) begin
			HPreg_pheonix <= Pheonix_start_HP;
		end
		
		else if ( bullet & pheonix ) begin	//detected collision of projectile and boss
			if ( !same_hit ) begin
				if (HPreg_pheonix >= 10) begin
					HPreg_pheonix <= HPreg_pheonix - 4'd10;		//decrease HP bar
				end
				else if (HPreg_pheonix < 10) begin
					HPreg_pheonix <= 0;
				end
			end
		end
	end
	
	always @ (posedge clk) begin
		if (rst) same_hit <= 1'b0;
		else if (bullet & pheonix) begin
			same_hit <= 1'b1;		// takes 1 clock cycle to assert
		end else if (bullet & !pheonix) begin
			same_hit <= 1'b0;
		end
	end

	assign enablecat1 = ~hit_cat1;
	assign enablecat2 = ~hit_cat2;
	assign enablecat3 = ~hit_cat3;
	assign enablecat4 = ~hit_cat4;
	assign enablecat5 = ~hit_cat5;
	assign enablecat6 = ~hit_cat6;
	assign enablecat7 = ~hit_cat7;
	assign enablecat8 = ~hit_cat8;

	assign boss_enable = boss_mode;
	assign splash_enable = splash_mode;

//a null bar, the hp and health are set to this when we dont want them displayed on screen
	assign null_bar = ~blank & (hcount >= 700 & hcount <= 700 & vcount >= 600 & vcount <= 600);
	
	assign HP_Bar_pheonix = hp_off? null_bar : (~blank & (hcount >= Xpheonix_HP_Bar & hcount <= Xpheonix_HP_Bar+HPreg_pheonix 
												& vcount >= Ypheonix_HP_Bar & vcount <= Ypheonix_HP_Bar+15));
	assign HP_Bar_inverse = hp_off? null_bar : (~blank & (hcount >= Xpheonix_HP_Bar+HPreg_pheonix & hcount <= Xpheonix_HP_Bar+Pheonix_start_HP 
												& vcount >= Ypheonix_HP_Bar & vcount <= Ypheonix_HP_Bar+15));
	assign HP_Bar_Border = hp_off? null_bar : (~blank & (hcount >= Xpheonix_HP_Bar-3 & hcount <= Xpheonix_HP_Bar+Pheonix_start_HP+3
												& vcount >= Ypheonix_HP_Bar-3 & vcount <= Ypheonix_HP_Bar+18));
												
	assign score_bar = score_off? null_bar : (~blank & (hcount >= Xscore_bar & hcount <= Xscore_bar+20 
												& vcount >= Yscore_bar-(10*enemies_killed) & vcount <= Yscore_bar));
	assign score_border = score_off? null_bar : (~blank & (hcount >= Xscore_bar-3 & hcount <= Xscore_bar+23
												& vcount >= Yscore_bar-(10*enemy_kill_goal)-3 & vcount <= Yscore_bar+3));

	assign an_enemy_sprite = sprite1 | sprite2 | sprite3 | sprite4 | sprite5 | sprite6 | sprite7 | sprite8;

endmodule
